package StudyCode.Component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.util.Date;

/**
 * @author lixiaoyao
 * @date 2018/11/22 0022、上午 8:26
 * @description option窗体
 */
public class OptionDialogFrame extends JFrame {

	private ButtonPanel panelType;
	private ButtonPanel panelMessage;
	private ButtonPanel panelMessageType;
	private ButtonPanel panelOptionType;
	private ButtonPanel panelOptions;
	private ButtonPanel panelInput;

	private String stringMessage = "Message";
	private Object objectMessage = new Date();
	private Component componentMessage = new SampleComponent();

	public OptionDialogFrame() {
		JPanel panelGrid = new JPanel();
		panelGrid.setLayout(new GridLayout(2, 2));

		this.panelType = new ButtonPanel("Type", "Message", "Confirm", "Option", "Input");
		this.panelMessageType = new ButtonPanel("Message Type", "ERROR_MESSAGE", "INFORMATION_MESSAGE", "WARNING_MESSAGE", "QUESTION_MESSAGE", "PLAIN_MESSAGE");
		this.panelMessage = new ButtonPanel("Message", "String", "Icon", "Component", "Other", "Object[]");
		this.panelOptionType = new ButtonPanel("Confirm", "DEFAULT_OPTION", "YES_ON_OPTION", "YES_NO_CANCEL_OPTION", "OK_CANCEL_OPTION");
		this.panelOptions = new ButtonPanel("Option", "String[]", "Icon[]", "Object[]");
		this.panelInput = new ButtonPanel("Input", "Text field", "Combo box");

		panelGrid.add(this.panelType);
		panelGrid.add(this.panelMessageType);
		panelGrid.add(this.panelMessage);
		panelGrid.add(this.panelOptionType);
		panelGrid.add(this.panelOptions);
		panelGrid.add(this.panelInput);

		// add a panel with a show button

		JPanel panelShow = new JPanel();
		JButton buttonShow = new JButton("Show");
		buttonShow.addActionListener(new ShowAction());
		panelShow.add(buttonShow);

		add(panelGrid, BorderLayout.CENTER);
		add(panelShow, BorderLayout.SOUTH);
		pack();
	}

	public Object getMessage() {
		String s = this.panelMessage.getSelection();
		if (s.equals("String")) {
			return this.stringMessage;
		} else if (s.equals("Icon")) {
			return "";
		} else if (s.equals("Component")) {
			return this.componentMessage;
		} else if (s.equals("Object[]")) {
			return new Object[]{this.stringMessage, this.componentMessage, this.objectMessage};
		} else if (s.equals("Other")) {
			return this.objectMessage;
		} else {
			return null;
		}
	}

	public Object[] getOptions() {
		String s = this.panelOptions.getSelection();
		if (s.equals("String[]")) {
			return new String[]{"Yellow", "Blue", "Red"};
		} else if (s.equals("Icon[]")) {
			return null;
		} else if (s.equals("Object[]")) {
			return new Object[]{this.stringMessage, this.componentMessage, this.objectMessage};
		} else {
			return null;
		}
	}

	public int getType(ButtonPanel buttonPanel) {
		String s = buttonPanel.getSelection();
		try {
			return JOptionPane.class.getField(s).getInt(null);
		} catch (Exception ex) {
			return -1;
		}
	}

	private class ShowAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (panelType.getSelection().equals("Confirm")) {
				JOptionPane.showConfirmDialog(OptionDialogFrame.this, getMessage(), "Title", getType(panelOptionType), getType(panelMessageType));
			} else if (panelType.getSelection().equals("Input")) {
				if (panelInput.getSelection().equals("Text field")) {
					JOptionPane.showInputDialog(OptionDialogFrame.this, getMessage(), "Title", getType(panelMessageType));
				} else {
					JOptionPane.showInputDialog(OptionDialogFrame.this, getMessage(), "Title", getType(panelMessageType), null, new String[]{"Yellow", "Blue", "Red"}, "Blue");
				}
			} else if (panelType.getSelection().equals("Message")) {
				JOptionPane.showMessageDialog(OptionDialogFrame.this, getMessage(), "Title", getType(panelMessageType));
			} else if (panelType.getSelection().equals("Option")) {
				JOptionPane.showOptionDialog(OptionDialogFrame.this, getMessage(), "Title", getType(panelOptionType), getType(panelMessageType), null, getOptions(), getOptions()[0]);
			}
		}
	}

	public static void main(String[] args) {
		OptionDialogFrame optionDialogFrame = new OptionDialogFrame();
		optionDialogFrame.setSize(800, 600);
		optionDialogFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		optionDialogFrame.setVisible(true);
	}
}

class SampleComponent extends JComponent {

	@Override
	protected void paintComponent(Graphics graphics) {
		Graphics2D graphics2D = (Graphics2D) graphics;
		Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, getWidth() - 1, getHeight() - 1);
		graphics2D.setPaint(Color.YELLOW);
		((Graphics2D) graphics).fill(rectangle2D);
		graphics2D.setPaint(Color.BLUE);
		graphics2D.draw(rectangle2D);
	}

	@Override
	public Dimension getPreferredSize() {
		return new Dimension(10, 10);
	}

}